<template>
  <div>
    <a-card :bordered="false" style="margin-bottom: 10px;">
      <!-- <OrderIndexForm ref="OrderIndexForm" @ok="getList"/> -->
      <!-- 条件搜索 -->
      <div class="table-page-search-wrapper">
        <a-form :labelCol="labelCol" :wrapperCol="wrapperCol" ref="queryForm">
          <a-row :gutter="32">
            <a-col :span="6">
              <a-form-item label="日期">
                <a-range-picker format="YYYY-MM-DD" v-model="orderDate"  :show-today="true"/>
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="查询条件">
                <a-input  placeholder="查询条件"  v-model="queryParam.condition" />
              </a-form-item>
            </a-col>
            <a-col>
              <span class="table-page-search-submitButtons" style="float: right;">
                <a-button type="primary" @click="handleQuery"><a-icon type="search" />查询</a-button>
                <a-button style="margin-left: 8px" @click="resetQuery"><a-icon type="redo" />重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false" class="table-card">
      <!-- :customRow="customRow" -->
      <advance-table 
        :scroll="{x:true}"
        :title="title" :pagination="{
        current: queryParam.pageNum,
        pageSize: queryParam.pageSize,
        total: total,
        showSizeChanger: true,
        showLessItems: true,
        showQuickJumper: true,
        showTotal: (total, range) => `第 ${range[0]}-${range[1]} 条，总计 ${total} 条`,
        onChange: changeSize,
        onShowSizeChange: onShowSizeChange
      }" tableKey="base-bizOrder-index-table"  rowKey="id" @refresh="getList"
      
        :columns="columns" :data-source="dataList" :loading="loading" :format-conditions="true">
        <div class="table-operations" slot="button">
          <a-button type="primary"  @click='print'>
            <a-icon />打印
          </a-button>
          
          <a-button type="primary" v-hasPermi="['biz:bizReportOrder:export']" :loading="downLoading"  @click='exportExcel'>
            <a-icon />导出
          </a-button>
        </div>
        
        <!-- <span v-for="(item,index) in 10" :key="index" :slot="'cost'+(index + 1) + 'Slot'" slot-scope="{ record }">
          <span>{{ record['cost'+(index + 1)] || 0}}</span>
        </span>
        <span slot="profitSlot" slot-scope="{ record}">
          {{ getProfit(record) }}
        </span> -->
      </advance-table>
    </a-card>
    <ant-modal
      :visible="open"
      modal-title="打印销售明细表"
      :adjust-size="false"
      @cancel="cancel"
      modalHeight="550"
      modalWidth="1000">
      <a-row slot="content">
        <iframe height='425' width='985' style='border:none;' :src='printUrl' ></iframe>
      </a-row>

      <div slot="footer">
        <a-button @click="cancel">关闭</a-button>
      </div>
    </ant-modal>
  </div>
</template>
<script>
import AdvanceTable from '@/components/pt/table/AdvanceTable'
import { listOrderStat } from '@/api/biz/bizOrderStat'
import AntModal from '@/components/pt/dialog/AntModal'
import storage from 'store'
  import tableDragResizeMixin from '@/mixins/tableDragResizeMixin'
  import listMixin from '@/mixins/listMixin';
import { ACCESS_TOKEN } from '@/store/mutation-types'
// import OrderIndexForm from './components/OrderIndexForm';
export default {
  name: 'OrderStat',
  components: {
    AdvanceTable,
    AntModal,
    // OrderIndexForm
  },
    mixins:[tableDragResizeMixin,listMixin],
  data() {
    return {
      printUrl:'/api/biz/bizOrder/printOrderStat?Authorization='+storage.get(ACCESS_TOKEN),
      printUrlPath:'/api/biz/bizOrder/printOrderStat?Authorization='+storage.get(ACCESS_TOKEN),
      open:false,
      orderDate: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中的主键集合
      selectedRowKeys: [],
      // 选中的数据集合
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // label的百分比
      labelCol: { span: 6 },
      // 内容区域的百分比
      wrapperCol: { span: 18 },
      statusOptions: [],
      payOptions: [],
      // 表格数据
      dataList: [],
      // 仓库字典
      warehouseIdOptions: [],
      // 供应商列表
      supplierOptions: [],
      // 查询参数
      title:'销售报表',
      listFunc:(param)=>listOrderStat(param),
      queryParam: {
        pageNum: 1,
        pageSize: 11,
        purchaseDate: undefined,
        supplierCode: undefined,
        status: undefined
      },
      columns: [
        {
          title: '下单日期',
          dataIndex:'orderDate',
          align: 'center'
        },
        {
          title: '生产日期',
          dataIndex:'createTime',
          align: 'center'
        },
        {
          title: '安装日期',
          dataIndex:'installDate',
          align: 'center'
        },
        {
          title: '编号',
          dataIndex:'orderNumber',
          align: 'center'
        },
        {
          title: '客户名称',
          dataIndex:'customerName',
          align: 'center'
        },
        {
          title: '设计师',
          dataIndex:'biller',
          align: 'center'
        },
        {
          title: '地址',
          dataIndex:'address',
          align: 'center'
        },
        {
          title: '成品仓位',
          dataIndex:'productWarehouse',
          align: 'center'
        },
        {
          title: '品牌',
          dataIndex:'brand',
          align: 'center',
        },
        {
          title: '颜色',
          dataIndex:'color',
          align: 'center'
        },
        {
          title: '规格',
          dataIndex:'spec',
          align: 'center'
        },
        {
          title: '型号',
          dataIndex:'model',
          align: 'center'
        },
        {
          title: '数量',
          dataIndex:'number',
          align: 'center'
        },
        {
          title: '单位',
          dataIndex:'unit',
          align: 'center'
        },
        {
          title: '单价',
          dataIndex:'price',
          align: 'center'
        },
        {
          title: '成本单价',
          dataIndex:'balance',
          align: 'center'
        },
        {
          title: '金额',
          dataIndex:'amount',
          align: 'center'
        },
        {
          title: '利润',
          dataIndex:'profit',
          align: 'center',
          scopedSlots: { customRender: 'profitSlot' },
        },
        // {
        //   title: '费用1',
        //   dataIndex:'cost1',
        //   align: 'center',
        //   scopedSlots: { customRender: 'cost1Slot' },
        // },
        // {
        //   title: '费用2',
        //   dataIndex:'cost2',
        //   align: 'center',
          
        //   scopedSlots: { customRender: 'cost2Slot' },
        // },
        // {
        //   title: '费用3',
        //   dataIndex:'cost3',
        //   align: 'center',
          
        //   scopedSlots: { customRender: 'cost3Slot' },
        // },
        // {
        //   title: '费用4',
        //   dataIndex:'cost4',
        //   align: 'center',
          
        //   scopedSlots: { customRender: 'cost4Slot' },
        // },
        // {
        //   title: '费用5',
        //   dataIndex:'cost5',
        //   align: 'center',
          
        //   scopedSlots: { customRender: 'cost5Slot' },
        // },
        // {
        //   title: '费用6',
        //   dataIndex:'cost6',
        //   align: 'center',
          
        //   scopedSlots: { customRender: 'cost6Slot' },
        // },
        // {
        //   title: '费用7',
        //   dataIndex:'cost7',
        //   align: 'center',
          
        //   scopedSlots: { customRender: 'cost7Slot' },
        // },
        // {
        //   title: '费用8',
        //   dataIndex:'cost8',
        //   align: 'center',
          
        //   scopedSlots: { customRender: 'cost8Slot' },
        // },
        // {
        //   title: '费用9',
        //   dataIndex:'cost9',
        //   align: 'center',
          
        //   scopedSlots: { customRender: 'cost9Slot' },
        // },
        // {
        //   title: '费用10',
        //   dataIndex:'cost10',
        //   align: 'center',
          
        //   scopedSlots: { customRender: 'cost10Slot' },
        // },
        {
          title: '业务员',
          dataIndex:'customernan',
          align: 'center'
        },
        {
          title: '备注1',
          align: 'center',
          dataIndex:'remark',
          width: '10%'
        },
        {
          title: '备注2',
          align: 'center',
          dataIndex:'remarkA',
          width: '10%'
        },
        {
          title: '备注3',
          align: 'center',
          dataIndex:'remarkB',
          width: '10%'
        }
      ]
    }
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询销售单列表 */
    getList() {
      if(this.orderDate && this.orderDate.length>0) {
        this.queryParam.startDate=this.orderDate[0].format('yyyy-MM-DD');
      } else {
        this.queryParam.startDate=undefined;
      }
      if(this.orderDate && this.orderDate.length>1) {
        this.queryParam.endDate = this.orderDate[1].format('yyyy-MM-DD');
      } else {
        this.queryParam.endDate=undefined;
      }
      let params = Object.assign({},this.queryParam,{pageSize:this.queryParam.pageSize - 1})
      this.loading = true
      listOrderStat(params).then(response => {
        this.dataList = response.data.list
        this.total = response.data.total
        this.loading = false
        this.tableAddTotalRow(this.columns,this.dataList)
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParam.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParam = {
        pageNum: 1,
        pageSize: 11,
        purchaseDate: undefined,
        supplierCode: undefined,
        status: undefined

      }
      this.handleQuery()
    },
    /** 翻页操作 */
    onShowSizeChange(current, pageSize) {
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 翻页操作 */
    onSizeChange(current, size) {
      this.queryParam.pageNum = 1
      this.queryParam.pageSize = size
      this.getList()
    },
    /** 翻页操作 */
    changeSize(current, pageSize) {
      this.queryParam.pageNum = current
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 翻页操作 */
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      this.ids = this.selectedRows.map(item => item.id)
      this.single = selectedRowKeys.length !== 1
      this.multiple = !selectedRowKeys.length
    },
    getProfit(record){
      let costTotal = 0
      for (let key in record) {
        if (key.indexOf('cost') > -1) {
          costTotal += (record[key] || 0)
        }
      }

      return this.$toDecimal((record.price - record.balance) * record.number - costTotal)
    },
    cancel() {
      this.open=false;
    },
    print() {
      this.printUrl=this.printUrlPath+"&r="+Math.random();
      if(this.orderDate && this.orderDate.length>0) {
        this.printUrl+="&startDate="+this.orderDate[0].format('yyyy-MM-DD');
      }
      if(this.orderDate && this.orderDate.length>1) {
        this.printUrl+="&endDate="+this.orderDate[0].format('yyyy-MM-DD');
      }
      if(this.queryParam.condition) {
        this.printUrl+="&condition="+this.queryParam.condition;
      }
      this.open=true;
    },
    handleEdit(record){
      this.$refs.OrderIndexForm.handleEdit(record)
    },
    customRow (record) {
        return {
          on: {
            dblclick: () => {
              this.$nextTick(() => this.handleEdit(record))
            }
          }
        }
      }
  }
}
</script>